package com.example.vv.model.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vv.R;
import com.example.vv.frameWork.entity.LoginEntity;
import com.example.vv.frameWork.mvp.MvpActivity;
import com.example.vv.frameWork.until.InputTextHelper;
import com.example.vv.frameWork.until.SharePUntitl;
import com.example.vv.model.forget.ForgetAct;
import com.example.vv.model.login.base.contract.LoginContract;
import com.example.vv.model.login.base.presenter.LoginPresenter;
import com.example.vv.model.register.RegisterAct;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginAct extends MvpActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {
    @BindView(R.id.close_img)
    ImageView closeImg;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.eye_iv)
    ImageView eyeIv;
    @BindView(R.id.forgot_password)
    TextView forgotPassword;
    @BindView(R.id.login_tv)
    TextView loginTv;
    @BindView(R.id.register)
    TextView register;

    @Override
    public Context getMContext() {
        return this;
    }

    @Override
    public int initLayout() {
        return R.layout.act_login;
    }

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        /*输入监听*/
        InputTextHelper.with(this)
                .setMain(loginTv)
                .addView(phoneEt)
                .addView(pwdEt)
                .setAlpha(true)
                .setListener(new InputTextHelper.OnInputTextListener() {
                    @Override
                    public boolean onInputChange(InputTextHelper helper) {
                        return presenter.isFormat();
                    }
                }).build();
        String stringExtra = getIntent().getStringExtra("phone");
        phoneEt.setText(stringExtra);
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleVIew() {
        return null;
    }

    @OnClick({R.id.register, R.id.close_img, R.id.forgot_password,R.id.login_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register:
                //注册
                startActivity(new Intent(LoginAct.this, RegisterAct.class));
                break;
            case R.id.close_img:
                finish();
                break;
            case R.id.forgot_password:
                //忘记密码
                startActivity(new Intent(LoginAct.this, ForgetAct.class));
                break;
            case  R.id.login_tv:
                //登录
                presenter.login();
                break;
        }
    }

    @Override
    public String getPhone() {
        return phoneEt.getText().toString();
    }

    @Override
    public String getUserPassWord() {
        return pwdEt.getText().toString();
    }


    @Override
    public void setLoginEntity(LoginEntity loginEntity) {
        if(loginEntity.getUserBean()==null){
            //登录成功
//            startActivity();
            SharePUntitl.addUserInfo(this,loginEntity);
            finish();
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }else{
            //登录失败
            Toast.makeText(this, "错误账号或密码", Toast.LENGTH_SHORT).show();
        }
    }
}
